/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.skillengine.effect;

import com.aionengine.gameserver.controllers.observer.ActionObserver;
import com.aionengine.gameserver.controllers.observer.ObserverType;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.stats.container.StatEnum;
import com.aionengine.gameserver.network.aion.serverpackets.SM_ATTACK_STATUS.LOG;
import com.aionengine.gameserver.network.aion.serverpackets.SM_ATTACK_STATUS.TYPE;
import com.aionengine.gameserver.skillengine.model.Effect;
import com.aionengine.gameserver.skillengine.model.HealType;

import javax.xml.bind.annotation.XmlAttribute;


/**
 * @author kecimis
 */
public class CaseHealEffect extends AbstractHealEffect {

    @XmlAttribute(name = "cond_value")
    protected int condValue;
    @XmlAttribute
    protected HealType type;

    @Override
    protected int getCurrentStatValue(Effect effect) {
        if (type == HealType.HP)
            return effect.getEffected().getLifeStats().getCurrentHp();
        else if (type == HealType.MP)
            return effect.getEffected().getLifeStats().getCurrentMp();

        return 0;
    }

    @Override
    protected int getMaxStatValue(Effect effect) {
        if (type == HealType.HP)
            return effect.getEffected().getGameStats().getMaxHp().getCurrent();
        else if (type == HealType.MP)
            return effect.getEffected().getGameStats().getMaxMp().getCurrent();

        return 0;
    }

    @Override
    public void applyEffect(Effect effect) {
        effect.addToEffectedController();
    }

    @Override
    public void endEffect(Effect effect) {
        ActionObserver observer = effect.getActionObserver(position);
        if (observer != null)
            effect.getEffected().getObserveController().removeObserver(observer);
    }

    @Override
    public void startEffect(final Effect effect) {
        ActionObserver observer = new ActionObserver(ObserverType.ATTACKED) {

            @Override
            public void attacked(Creature creature) {
                calculateHeal(effect);
            }
        };
        effect.getEffected().getObserveController().addObserver(observer);
        effect.setActionObserver(observer, position);
        calculateHeal(effect);
    }

    private void calculateHeal(final Effect effect) {
        final int valueWithDelta = value + delta * effect.getSkillLevel();
        final int currentValue = getCurrentStatValue(effect);
        final int maxValue = getMaxStatValue(effect);
        if (currentValue <= (maxValue * condValue / 100)) {
            int possibleHealValue = 0;
            if (percent)
                possibleHealValue = maxValue * valueWithDelta / 100;
            else
                possibleHealValue = valueWithDelta;

            int finalHeal = effect.getEffected().getGameStats().getStat(StatEnum.HEAL_SKILL_BOOST, possibleHealValue)
                    .getCurrent();
            finalHeal = effect.getEffected().getGameStats().getStat(StatEnum.HEAL_SKILL_DEBOOST, finalHeal).getCurrent();
            finalHeal = maxValue - currentValue < finalHeal ? (maxValue - currentValue) : finalHeal;

            if (type == HealType.HP && effect.getEffected().getEffectController().isAbnormalSet(AbnormalState.DISEASE))
                finalHeal = 0;

            // apply heal
            if (type == HealType.HP)
                effect.getEffected().getLifeStats().increaseHp(TYPE.HP, finalHeal, effect.getSkillId(), LOG.REGULAR);
            else if (type == HealType.MP)
                effect.getEffected().getLifeStats().increaseMp(TYPE.MP, finalHeal, effect.getSkillId(), LOG.REGULAR);
            effect.endEffect();
        }
    }
}
